#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
using namespace std;
const int port = 8888;
const string SERVER_IP = "127.0.0.1";


void* thread_func(void* arg)
{
    int sockfd = (int)arg;
}
int main()
{
    //创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    sockaddr_in server_addr = {
        .sin_family = AF_INET,
        .sin_port = htons(port),     //端口号
        .sin_addr.s_addr = inet_addr("127.0.0.1")
    };
    //绑定地址
    bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    
    //监听连接
    if (listen(sockfd, 5)){
        perror("listen");
    }

    //接受连接
    int client_socket = accept(sockfd, NULL, NULL);
    char buffer[1024];
    char * send_buffer = "hello, i am server";
    while (true)
    {
        int bytes = recv(client_socket, buffer, sizeof(buffer), 0);
        if (bytes <= 0){

            break;
        }
        std::cout<<"recv frome client:"<<buffer<<endl;
        send(client_socket, send_buffer, strlen(send_buffer), 0);
    }
    close(client_socket);
    close(sockfd);

    pthread_t tid;
    while(1) {
        int client_socket = accept(sockfd, NULL, NULL);
        pthread_create(&tid, NULL, thread_func, (void *)client_socket);
    }
}